﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Serialization;

namespace WebberCross.JavaScriptBridge.WebParts
{
    /// <summary>
    /// This is a base class for Silverlight webparts
    /// </summary>
    public class SilverlightWebPartBase : WebPartBase
    {
        private string _xapSource = "";
        private string _startPage = "";
        private string _jsClientID = "";
        private string _jsClientSourceID = "";

        // Public properties visible to SharePoint UI
        //
        [WebBrowsable(true),
            Category("Configuration"),
            Personalizable(PersonalizationScope.Shared),
            DefaultValue(""),
            WebDisplayName("XAP Source"),
            WebDescription("XAP Package Source URL")]
        public string XAPSource
        {
            get { return _xapSource; }
            set { _xapSource = value; }
        }

        [WebBrowsable(true),
            Category("Configuration"),
            Personalizable(PersonalizationScope.Shared),
            DefaultValue(""),
            WebDisplayName("Silverlight Start Page"),
            WebDescription("Silverlight Start Page")]
        public string StartPage
        {
            get { return _startPage; }
            set { _startPage = value; }
        }

        [WebBrowsable(true),
            Category("Configuration"),
            Personalizable(PersonalizationScope.Shared),
            DefaultValue(""),
            WebDisplayName("Javascript Client ID"),
            WebDescription("Used for JavaScript Bridge")]
        public string JSClientID
        {
            get { return _jsClientID; }
            set { _jsClientID = value; }
        }

        [WebBrowsable(true),
            Category("Configuration"),
            Personalizable(PersonalizationScope.Shared),
            DefaultValue(""),
            WebDisplayName("Javascript Client Source ID"),
            WebDescription("Used for JavaScript Bridge")]
        public string JSClientSourceID
        {
            get { return _jsClientSourceID; }
            set { _jsClientSourceID = value; }
        }

        public string JSUniqueClientID
        {
            get { return this.ClientID + "SL"; }
        }

        public SilverlightWebPartBase()
        {

        }

        // This injects JavaScript into the page so register the silverlight control to the bridge
        protected void RegisterJSBridge()
        {
            // Create JSClientObject
            JSClientObject jsco = new JSClientObject(this.JSClientID, this.JSClientSourceID, this.JSUniqueClientID);

            HtmlGenericControl scriptBlock = new HtmlGenericControl("script");
            scriptBlock.Attributes.Add("type", "text/javascript");
            scriptBlock.InnerHtml = string.Format("registerClient('{0}', '{1}');", base.ClientID, jsco.ToJASON());
            this.Controls.Add(scriptBlock);
        }

        
    }
}
